home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////
- // hzip.cpp: Huffman file compressor
- // Copyright (c) 1991 Azarona Software
- // All rights reserved.
- ////////////////////////////////////////////////////////
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "huffenc.h"
-
- huff_encoder huff(256, 16); // 256 symbols, 16 bit max code lengths
-
- main(int argc, char *argv[])
- {
- FILE *f, *g;
-
- if (argc < 3) {
- printf("Usage: hzip infile outfile\n");
- return 0;
- }
-
- f = fopen(argv[1], "rb");
- if (f == NULL) {
- printf("Couldn't open input file: %s\n", argv[1]);
- return 0;
- }
-
- g = fopen(argv[2], "wb");
- if (g == NULL) {
- printf("Couldn't create output file: %s\n", argv[2]);
- return 0;
- }
-
- printf("Collecting frequencies ...\n");
- while(!feof(f)) {
- int c = fgetc(f);
- if (c != -1) huff.counts[c]++;
- }
-
- printf("Generating huffman codes ...\n");
- if (!huff.generate_codes()) {
- printf("No solution to package-merge. Codes could not "
- "be generated.\n");
- exit(1);
- }
-
- huff.print_data(1);
-
- fputs("HZIP 1.0", g);
- huff.dump_codes(g, 8); // 8 is sizeof(signature)
-
- printf("Compressing file ...\n");
- fseek(f, 0, SEEK_SET);
- huff.encode(g, f);
-
- fclose(f);
- fclose(g);
- }
-